package sdk.webview.fmc.com.fmcsdk.check.activity;

import static sdk.webview.fmc.com.fmcsdk.activity.H5Activity.EVENT_BUS_NOTIFY_H5_BACK;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.activity.H5Activity;
import sdk.webview.fmc.com.fmcsdk.base.BaseActivity;
import sdk.webview.fmc.com.fmcsdk.check.adapter.OperationHistoryAdapter;
import sdk.webview.fmc.com.fmcsdk.check.bean.ActivityJsonBean;
import sdk.webview.fmc.com.fmcsdk.check.bean.WorkHistoryBean;
import sdk.webview.fmc.com.fmcsdk.check.interfaces.WorkHistoryView;
import sdk.webview.fmc.com.fmcsdk.check.persenter.WorkHistoryPersenter;
import sdk.webview.fmc.com.fmcsdk.db.DatabaseManager;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.GsonUtil;
import sdk.webview.fmc.com.fmcsdk.util.HandleJsEvent;
import sdk.webview.fmc.com.fmcsdk.util.KLog;
import sdk.webview.fmc.com.fmcsdk.util.NetworkUtils;
import sdk.webview.fmc.com.fmcsdk.util.StringUtils;
import sdk.webview.fmc.com.fmcsdk.util.ToastUtils;

public class OperationHistoryActivity extends BaseActivity<WorkHistoryView, WorkHistoryPersenter> implements WorkHistoryView {


    private TextView mTvTitle;


    private OperationHistoryAdapter workHistoryAdapter;
    private ImageView mIvBack;
    private RecyclerView mRvHistory;
    String id, projId;


    private WorkHistoryBean workHistoryBean;
    private SwipeRefreshLayout mSelView;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_operation_history;
    }

    @Override
    protected void init(Bundle savedInstanceState) {

        // 在 Activity 的 onCreate() 方法中设置状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(ContextCompat.getColor(this, R.color.color_DC3C26)); // 设置状态栏颜色
        }

        id = getIntent().getStringExtra("data");
        projId = getIntent().getStringExtra("projId");
        initView();
        initListener();
        requestApi();
        KLog.e(id +"---"+projId+"--"+getClass().getSimpleName());
        ActivityJsonBean bean = new DatabaseManager(context).getUserByName(getClass().getSimpleName() + id + projId);
        if (bean != null) {
            if (!TextUtils.isEmpty(bean.getJson())) {
                workHistoryBean = GsonUtil.GsonToBean(bean.getJson(), WorkHistoryBean.class);
                KLog.e(GsonUtil.BeanToJson(workHistoryBean));
                getHistory(workHistoryBean);
            }
        } else {
            if (!NetworkUtils.isAvailable(context)) {
                ToastUtils.showShort(context,getString(R.string.qingzaixian));
                finish();
            }
        }


    }

    private void requestApi() {
        if (!NetworkUtils.isAvailable(context)) return;
        presenter.getDetails(id, projId);
    }


    private void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText(getResources().getString(R.string.Operation_History));
        mIvBack = findViewById(R.id.iv_back);
        mRvHistory = findViewById(R.id.rv_history);


        mRvHistory.setLayoutManager(new LinearLayoutManager(context));
        workHistoryAdapter = new OperationHistoryAdapter(R.layout.item_operation_history);
        workHistoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WorkHistoryBean.RecordsDTO item = workHistoryAdapter.getItem(position);
                if (null != item) {
                    if ((item.getOperateType().equals("SUBCHECK") || (item.getOperateType().equals("SUBAPPR")) && !StringUtils.isEmpty(item.getWfEntityId()))) {
                        //跳转详情
                        Intent intent = new Intent(context, ApprovalAcceptanceHistoryActivity.class);
                        intent.putExtra("wfEntityId", item.getWfEntityId());
                        intent.putExtra("ownerId", item.getOrderId());
                        intent.putExtra("projId", item.getProjId());
                        startActivity(intent);
                    }
//                    else {
//                        String baseUrl = PreferenceManager.getDefaultSharedPreferences(context).getString(Constant.BASE_ADDRESS_URL, "");
//                        if (item.getType().equals("8") || item.getType().equals("7") && !StringUtils.isEmpty(item.getOrderId())){
//                            //跳转巡检详情
//                            Intent intent = new Intent(context, CheckDetailsActivity.class);
//                            intent.putExtra("id",item.getOrderId());
//                            intent.putExtra("isFromDeviceHealthCard",true);
//                            startActivity(intent);
//
//                        }else if (item.getType().equals("5") && !StringUtils.isEmpty(item.getOrderId())){
//                            String h5Url = baseUrl + "/h5/mytask/" + "?accessToken=" + UserHelper.getToken()+"#/reportSrtask/detail/"+item.getRefId()+"?from=app";
//                            goH5Activity(h5Url);
//
//                        }else if (item.getType().equals("4") && !StringUtils.isEmpty(item.getOrderId())){
//                            String h5Url = baseUrl + "/h5/mytask/" + "?accessToken=" + UserHelper.getToken()+"#/workDetail/"+item.getOrderId()+"?from=app";
//                            goH5Activity(h5Url);
//                        }
//                    }
                }

            }
        });


        mRvHistory.setAdapter(workHistoryAdapter);
        mSelView = findViewById(R.id.sel_view);
    }

    private void goH5Activity(String url) {
        Intent intents = new Intent();
        intents.putExtra(Constant.H5_URL, url);
        intents.setClass(context, H5Activity.class);
        startActivity(intents);
    }

    private void initListener() {
        findViewById(R.id.iv_back).setOnClickListener(v -> {
            finish();
            EventBus.getDefault().post(new HandleJsEvent(EVENT_BUS_NOTIFY_H5_BACK, ""));
        });
        mSelView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestApi();
            }
        });
    }

    public static void startActivity(Context context, String id, String projId) {
        Intent intent = new Intent(context, OperationHistoryActivity.class);
        intent.putExtra("data", id);
        intent.putExtra("projId", projId);
        context.startActivity(intent);
    }


    @Override
    protected WorkHistoryPersenter createPresenter() {
        return new WorkHistoryPersenter();
    }

    @Override
    public void getHistory(WorkHistoryBean bean) {
        if (mSelView != null) {
            mSelView.setRefreshing(false);
        }
        new DatabaseManager(context).upsertJson(getClass().getSimpleName() + id + projId, GsonUtil.BeanToJson(bean));
        Collections.reverse(bean.getRecords());
        workHistoryAdapter.setNewData(bean.getRecords());
    }


}
